var commonDialog = new Dialog();
var commonAjax = new Ajax();
var commonJson = {};
var companyName = "";
$(function() {
	loadScript(appPath + "/project-yddemo/common/locale/Common_" + appLocale + ".js");
	commonJson.units = getUnits();
	commonJson.majors = getMajors();
	commonJson.conditions = getConditions();
	commonJson.valueTypes = getValueTypes();
});

function getUnits() {
	var units;
	$.ajax({
		url : requestPath + "/common/comUnits/findAllUnits", // 请求url*
		async : false, // 是否异步*
		dataType : "JSON",
		type : "GET", // 请求类型*
		success : function(data) {
			units = data;
		},
		error : function() {
			commonDialog.alertDialog(commonLocale.text.getUnitError);
		}
	});

	return units;
}

function getUnitOptions() {
	var units = getUnits();
	var options = [];
	for (var i = 0; i < units.length; i++) {
		var json = {};
		json.value = units[i].id;
		json.text = units[i].name;
		options.push(json);
	}
	return options;
}

//这个获取的公司不全
function getCompanys_old() {
	var companys;
	$.ajax({
		url : requestPath + "/system/companyOrganizations/companys/getAllCompanys",
		async : false, // 是否异步*
		dataType : "JSON",
		type : "GET", // 请求类型*
		success : function(data) {
			companys = data;
		},
		error : function() {
			commonDialog.alertDialog(commonLocale.text.getCompanyError);
		}
	});

	return companys;
}

function getCompanys() {
	var companys;
	$.ajax({
		url : requestPath + "/yddemo/dataDays/getAllCompanys",
		async : false, // 是否异步*
		dataType : "JSON",
		type : "GET", // 请求类型*
		success : function(data) {
			companys = data;
		},
		error : function() {
			commonDialog.alertDialog(commonLocale.text.getCompanyError);
		}
	});

	return companys;
}

function getCompanyOptions() {
	var companys = getCompanys();
	var options = [];
	for (var i = 0; i < companys.length; i++) {
		var json = {};
		json.value = companys[i].id;
		json.text = companys[i].name;
		options.push(json);
	}
	return options;
}

//添加获取当前登陆用户对应的公司
function getCurrentUserCompany(){
	var company;
	$.ajax({
		url : requestPath + "/yddemo/dataDays/findCompany",
		async : false, // 是否异步*
		dataType : "JSON",
		type : "GET", // 请求类型*
		success : function(data) {
			company = data;
		},
		error : function() {
			commonDialog.alertDialog(commonLocale.text.getUserCompanyError);
		}
	});

	return company;
}


function getCompanysOptions() {
	var company = getCurrentUserCompany();
	var companys = getCompanys();
	var options = [];
	for (var i = 0; i < companys.length; i++) {
		if(i == 0){
			var json = {};
			json.value = company.id;
			json.text = company.name;
			options.push(json);
			
			json = {};
			json.value = "";
			json.text = "全部";
			options.push(json);
		}
		if(companys[i].id != company.id){
			var json = {};
			json.value = companys[i].id;
			json.text = companys[i].name;
			options.push(json);
		}
		
	}
	return options;
}

//用于环保月填报
function getCompanyForMonthOptions() {
	var company = getCurrentUserCompany();
	var companys = getCompanys();
	var options = [];
	for (var i = 0; i < companys.length; i++) {
		if(i == 0){
			var json = {};
			json.value = company.id;
			json.text = company.name;
			options.push(json);
			companyName = company.name;
//			json = {};
//			json.value = "";
//			json.text = "全部";
//			options.push(json);
		}
		if(companys[i].id != company.id){
			var json = {};
			json.value = companys[i].id;
			json.text = companys[i].name;
			options.push(json);
		}
		
	}
	return options;
}

function getMajors() {
	var majors;
	$.ajax({
		url : requestPath + "/common/comMajors/findAllMajors", // 请求url*
		async : false, // 是否异步*
		dataType : "JSON",
		type : "GET", // 请求类型*
		success : function(data) {
			majors = data;
		},
		error : function() {
			commonDialog.alertDialog(commonLocale.text.getMajorError);
		}
	});
	return majors;
}

function getMajorOptions() {
	var majors = getMajors();
	var options = [];

	for ( var i in majors) {
		var json = {};
		json.value = majors[i].id;
		json.text = majors[i].name;
		options.push(json);
	}
	return options;
}

function getEtlCondition(id) {
	var conditionObj;
	$.ajax({
		url : requestPath + "/etl/conditions/" + id, // 请求url*
		async : false, // 是否异步*
		dataType : "JSON",
		type : "GET", // 请求类型*
		success : function(data) {
			conditionObj = data;
		},
		error : function() {
			commonDialog.alertDialog(commonLocale.text.getEtlConditionError);
		}
	});
	return conditionObj;
}

function getConditions() {
	var conditions = commonAjax.sendAjax({ // 获取用户对象
		url : requestPath + "/etl/conditions/getAllConditions", // 请求url*
		async : false, // 是否异步*
		type : "GET" // 请求类型*
	});
	return conditions;
}

function getConditionOptions() {
	var conditions = getConditions();
	var options = [];
	for (var i = 0; i < conditions.length; i++) {
		var json = {};
		json.value = conditions[i].id;
		json.text = conditions[i].name;
		options.push(json);
	}
	return options;
}

function getValueTypes() {
	var valueTypes = commonAjax.sendAjax({ // 获取用户对象
		url : requestPath + "/system/dicts/types/SYS_SIS_ETL_VALUE_TYPE", // 请求url*
		async : false, // 是否异步*
		type : "GET" // 请求类型*
	});
	return valueTypes;
}

function checkDate(key, date) {
	var benchmarkDate;
	if (key == "beginTime") {
		benchmarkDate = $("[key = 'endTime']").val();
		if (benchmarkDate != null && benchmarkDate != "") {
			if (!TimeObjectUtil.compareDateEndTimeGTStartTime(date,
					benchmarkDate)) {
				commonDialog.alertDialog(commonLocale.text.timeError);
				$("[key = 'beginTime']").val("");
			}
		}
	} else if (key == "endTime") {
		benchmarkDate = $("[key = 'beginTime']").val();
		if (benchmarkDate != null && benchmarkDate != "") {
			if (!TimeObjectUtil.compareDateEndTimeGTStartTime(benchmarkDate,
					date)) {
				commonDialog.alertDialog(commonLocale.text.timeError);
				$("[key = 'endTime']").val("");
			}
		}
	}
}

// 初始化按钮
function initOprateButtons_old(action, divId) {
	var saveButtonDiv = "<div class='formInputDiv' width='100%' style='text-align:center'>"
			+ "<span class='formButton' onclick='closeWindow()'>"
			+ commonLocale.text.close
			+ "</span>"
			+ "<span class='formButton' action='"
			+ action
			+ "' onclick='saveData()'>"
			+ commonLocale.text.save
			+ "</span></div>";
	$("#" + divId).append(saveButtonDiv);
}

// 初始化按钮
function initOprateButtons(action, divId) {
	var saveButtonDiv = "<div class='formInputDiv' width='100%' style='text-align:center'>"
			+ "<span class='formButton bg-mainButton' onclick='closeWindow()'>"
			+ commonLocale.text.close
			+ "</span>"
			+ "<span class='formButton bg-mainButton' action='"
			+ action
			+ "' onclick='saveData()'>"
			+ commonLocale.text.save
			+ "</span></div>";
	$("#" + divId).append(saveButtonDiv);
}

function autoAdjustWindow() {
	try {
		var maxHeight = $(window.parent).height() - 70;
		var height = document.body.scrollHeight;
		var maxWidth = $(window.parent).width() - 70;
		var width = document.body.scrollWidth;
		if (height <= maxHeight) {
			window.frameElement.height = height;
			window.frameElement.style.height = height + "px";
			window.frameElement.parentElement.style.height = height + 41 + "px";
		} else {
			window.frameElement.height = maxHeight;
			window.frameElement.style.height = maxHeight + "px";
			window.frameElement.parentElement.style.height = maxHeight + 41
					+ "px";
		}
		if (width <= maxWidth) {
			window.frameElement.width = width;
			window.frameElement.style.width = width + "px";
			window.frameElement.parentElement.style.width = width + "px";
		} else {
			window.frameElement.width = maxWidth;
			window.frameElement.style.width = maxWidth + "px";
			window.frameElement.parentElement.style.width = maxWidth + "px";
		}
	} catch (e) {

	}
}

/**
 * 重设表格高度
 * 
 * @param
 * @return
 */
function resetTableHeight(tableDiv, arrayDiv) {
	var leftHeight = $(".dataListDiv").height() - getTotalHeight(arrayDiv);
	$("#" + tableDiv + "").height(leftHeight);
}

function getShiftRecord(time) {
	var shiftRecordObj;
	var data = {
		time : time
	}
	$.ajax({
		url : requestPath + "/shift/records/findRecord", // 请求url*
		async : false, // 是否异步*
		dataType : "JSON",
		type : "GET", // 请求类型*
		data : data,
		success : function(data) {
			shiftRecordObj = data;
		},
		error : function() {
			commonDialog.alertDialog(commonLocale.text.getShiftRecordError);
		}
	});
	return shiftRecordObj;
}

/**
 * ydDemo:查询维度
 */
function getDimensionType() {
	var dimensionTypeObj = commonAjax.sendAjax({
		url : requestPath + "/system/dicts/types/DIMENSION_TYPE", // 请求url*
		async : false, // 是否异步*
		type : "GET" // 请求类型*
	});
	return dimensionTypeObj;
}

function getDimensionTypeOptions() {
	var dimensionTypes = getDimensionType();
	var options = [];
	for (var i = 0; i < dimensionTypes.entrys.length; i++) {
		var json = {};
		json.value = dimensionTypes.entrys[i].code;
		json.text = dimensionTypes.entrys[i].name;
		options.push(json);
	}
	return options;
}

function getIndicatorDetail(indicators){
	var data = {};
	data.indicators = indicators.join(",");
	var detailObj = commonAjax.sendAjax({
		url : requestPath + "/yddemo/benchmarks/findIndicators",
		async : false, // 是否异步*
		type : "GET", // 请求类型*
		data : data 
	});
	return detailObj;
}

function getComUnit(unitId) {
	var comUnitObj = commonAjax.sendAjax({ // 获取用户对象
		url : requestPath + "/common/comUnits/" + unitId, // 请求url*
		async : false, // 是否异步*
		type : "GET" // 请求类型*
	});
	return comUnitObj;
}